/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatv2.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.rmi.RemoteException;

import javax.swing.JFileChooser;
import javax.swing.text.DefaultCaret;

/**
 *
 * @author Aldok-EEE
 */
public class WhisperTab extends javax.swing.JPanel {

	private ChatClientApp app = null;
	private GUI gui = null;
	private String loginDest = "";
    /**
     * Creates new form WhisperTab
     */
    public WhisperTab(ChatClientApp app, GUI gui, String loginDest) {
    	this.app = app;
    	this.gui = gui;
    	this.loginDest = loginDest;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane_ChatWhisper_Main = new javax.swing.JScrollPane();
        jTextArea_ChatWhisper_Main = new javax.swing.JTextArea();
        jScrollPane_ChatWhisper_Send = new javax.swing.JScrollPane();
        jTextArea_ChatWhisper_Send = new javax.swing.JTextArea();
        jButton_ChatWhisper_SendFile = new javax.swing.JButton();
        jButton_ChatWhisper_Send = new javax.swing.JButton();
        DefaultCaret caret = (DefaultCaret)jTextArea_ChatWhisper_Main.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        jTextArea_ChatWhisper_Main.setBackground(new java.awt.Color(230, 230, 230));
        jTextArea_ChatWhisper_Main.setColumns(20);
        jTextArea_ChatWhisper_Main.setEditable(false);
        jTextArea_ChatWhisper_Main.setRows(5);
        jScrollPane_ChatWhisper_Main.setViewportView(jTextArea_ChatWhisper_Main);

        jTextArea_ChatWhisper_Send.setColumns(20);
        jTextArea_ChatWhisper_Send.setRows(5);
        jScrollPane_ChatWhisper_Send.setViewportView(jTextArea_ChatWhisper_Send);

        jButton_ChatWhisper_SendFile.setText("Send a File");

        jButton_ChatWhisper_Send.setText("Send");

        jButton_ChatWhisper_SendFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	 jButton_ChatWhisper_SendFileActionPerformed(evt);
            }
        });
        
        jButton_ChatWhisper_Send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	 jButton_ChatWhisper_SendActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 407, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane_ChatWhisper_Main, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jButton_ChatWhisper_SendFile, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jScrollPane_ChatWhisper_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton_ChatWhisper_Send)))
                    .addContainerGap(22, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 307, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(6, 6, 6)
                    .addComponent(jButton_ChatWhisper_SendFile)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane_ChatWhisper_Main, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(27, 27, 27)
                            .addComponent(jButton_ChatWhisper_Send))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane_ChatWhisper_Send, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents
    //Getters
    public String getMessage(){
    	return jTextArea_ChatWhisper_Send.getText();
    }
    
    public void appendChatWhisper(String str){
    	jTextArea_ChatWhisper_Main.append(str+"\n");
    }
    
    // Events
    private void jButton_ChatWhisper_SendFileActionPerformed(java.awt.event.ActionEvent evt){
    	JFileChooser chooser = new JFileChooser();
    	chooser.showOpenDialog(new FileChooser());
    	chooser.getSelectedFile().getAbsolutePath();
    	File file = chooser.getSelectedFile();
    	try {
    		app.<File>send(this.loginDest, file);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private void jButton_ChatWhisper_SendActionPerformed(java.awt.event.ActionEvent evt){
    	try {
			app.getServer().send(this.loginDest, this.gui.getLogin()+" vous chuchote : "+getMessage());
			appendChatWhisper("je chuchote : "+ getMessage());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_ChatWhisper_Send;
    private javax.swing.JButton jButton_ChatWhisper_SendFile;
    private javax.swing.JScrollPane jScrollPane_ChatWhisper_Main;
    private javax.swing.JScrollPane jScrollPane_ChatWhisper_Send;
    private javax.swing.JTextArea jTextArea_ChatWhisper_Main;
    private javax.swing.JTextArea jTextArea_ChatWhisper_Send;
    // End of variables declaration//GEN-END:variables
}
